package com.titaniume.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @ProjectName: juc
 * @Package: com.titaniume.readwrite
 * @ClassName: Demo1
 * @Author: huangrenfei
 * @Description: 可重入锁降级
 * @Date: 2021/11/24 15:27
 * @Version: 1.0
 */
public class Demo1 {

    public static void main(String[] args) {
        //可重入读写锁
        ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
        ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();//读锁
        ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock(); //写锁

        //降级锁

        //1.获取写锁
        writeLock.lock();
        System.out.println("titaniume");

        //2.获取读锁
        readLock.lock();
        System.out.println("reader");

        //3.释放写锁
        writeLock.unlock();

        //4.释放读锁
        readLock.unlock();


    }
}
